package org.dromara.system.job;

import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@JobExecutor(name = "CheckDeviceStatusJob")
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class CheckDeviceStatusJob {

    private final DeviceMapper deviceService;

    public ExecuteResult jobExecute(JobArgs jobArgs) {
        //查找设备表Device里面的last_ping超过30分钟的更改状态为0 如果他目前处于报警状态则不更新
//        deviceService.updateStatusIfLastPingExceeds30Minutes();
//        //变量状态也遵循上面
//        deviceService.updateVariableStatusIfLastPingExceeds30Minutes();
        //患有一种特殊情况 当一个设备下面的所有变量状态都为0 则她的状态也要为0
        List<Long> deviceIds = deviceService.getAllDevicesIds();
        if (deviceIds.isEmpty()) return ExecuteResult.success("批量更新状态成功");
        for (Long deviceId : deviceIds) {
            int nonZeroCount = deviceService.countNonZeroStatusVariables(deviceId);
            if (nonZeroCount == 0) {
                deviceService.updateDeviceStatusToZero(deviceId);
            }
        }

        return ExecuteResult.success("批量更新状态成功");
    }


}
